'use strict';
module.exports = {
	/**
	 * XXXnameXXX
	 * @url client/pages/kh/h5recharge 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			db,
			_
		} = util;
		let {
			uid,
		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------
		let appId = userInfo.appid
		let openId = userInfo.wx_openid['mp-weixin']
		let _add_time = new Date().getTime();
		let _add_time_str = vk.pubfn.timeFormat(new Date(), "yyyy-MM-dd hh:mm:ss");
		// let appId = appid
		// let openId = openid
		let apiUrl =
			`http://wap.hgylcm.com:9091/system//hg/api/orders?appId=${appId}&status=SUCCESS&openId=${openId}&pageNum=1&pageSize=20&completed=false`
		res = await uniCloud.httpclient.request(apiUrl, {
			method: 'GET',
			contentType: 'json', // 指定以application/json发送data内的数据
			dataType: 'json' // 指定返回值为json格式，自动进行parse
		})
		if (res.data.total == 0) {
			console.log("无订单");
			return res
		}
		let h5_recharge = res.data.data
		// console.log("res======", res);
		console.log("h5_recharge======", h5_recharge);
		// console.log("userInfo======", userInfo);

		for (let i = 0; i < h5_recharge.length; i++) {
			// console.log("h5_recharge======", h5_recharge[i]);
			let {
				orderNo,
				amount,
				point
			} = h5_recharge[i]
			let orderId = h5_recharge[i].id
			//获得用户当前金豆数
			let get_userinfobalance = await vk.baseDao.sum({
				dbName: "uni-id-users", // 表名
				fieldName: "balance", // 需要求和的字段名
				whereJson: { // 条件
					_id: userInfo._id
				}
			});

			let back_h5_recharge = await uniCloud.httpclient.request(
				"http://wap.hgylcm.com:9091/system/hg/api/orders/complete", {
					method: 'POST',
					data: {
						"orderId": h5_recharge[i].id,
						"openId": h5_recharge[i].openId
					},
					contentType: 'json', // 指定以application/json发送data内的数据
					dataType: 'json' // 指定返回值为json格式，自动进行parse
				});
			// console.log("1231231231", back_h5_recharge);
			//给用户添加金豆
			let add_jindou = await vk.baseDao.updateAndReturn({
				dbName: "uni-id-users",
				whereJson: {
					_id: userInfo._id
				},
				dataJson: {
					had_youhui: true, //had_youhui可以判断用户是否付费过，便于付费用户数量统计
					balance: _.inc(point)
				}
			});
			//添加金豆收支明细
			let add_jindoulist = await db.collection('a_jindou_detail').add({
				user_id: userInfo.user_id,
				nickname: userInfo.nickname || "未绑定",
				change_num: point, //增加的金豆数量
				changed_num: get_userinfobalance + point, //支付后金豆余额
				change_reason: "h5支付" + amount + "分钱充值" + point + "金豆",
				type: "income", //类型：expend:支出,income:收入。
				appid: appId || "",
				appname: userInfo.appname || "",
				openid: openId || "",
				_add_time: _add_time,
				_add_time_str: _add_time_str
			});
			//添加充值记录
			let add_h5_rechargelist = await db.collection('h5_recharge').add({
				out_trade_no: orderNo,
				user_id: userInfo._id,
				nickname: userInfo.nickname || '未绑定昵称',
				recharge_amount: amount,
				platform: "h5外接支付",
				pay_status: 0,
				bean: point,
				subject: "h5外接充值金豆",
				user_register_date: userInfo.register_date,
				appid: appId || "NoAppid",
				appname: userInfo.appname || "NoAppname",
				openid: openId || "",
				orderId: orderId,
				// province: userInfo.province, //省份
				// city: userInfo.city, //市
				_add_time: _add_time,
				_add_time_str: _add_time_str
			});
		}






		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}